package com.nmsl.sn.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nmsl.common.annotation.Log;
import com.nmsl.common.core.controller.BaseController;
import com.nmsl.common.core.domain.AjaxResult;
import com.nmsl.common.enums.BusinessType;
import com.nmsl.sn.domain.SnPrescriptionMedicine;
import com.nmsl.sn.service.ISnPrescriptionMedicineService;
import com.nmsl.common.utils.poi.ExcelUtil;
import com.nmsl.common.core.page.TableDataInfo;

/**
 * 记录患者处方药品Controller
 * 
 * @author sn
 * @date 2025-02-21
 */
@RestController
@RequestMapping("/sn/medicine")
public class SnPrescriptionMedicineController extends BaseController
{
    @Autowired
    private ISnPrescriptionMedicineService snPrescriptionMedicineService;

    /**
     * 查询记录患者处方药品列表
     */
    @PreAuthorize("@ss.hasPermi('sn:medicine:list')")
    @GetMapping("/list")
    public TableDataInfo list(SnPrescriptionMedicine snPrescriptionMedicine)
    {
        startPage();
        List<SnPrescriptionMedicine> list = snPrescriptionMedicineService.selectSnPrescriptionMedicineList(snPrescriptionMedicine);
        return getDataTable(list);
    }

    /**
     * 导出记录患者处方药品列表
     */
    @PreAuthorize("@ss.hasPermi('sn:medicine:export')")
    @Log(title = "记录患者处方药品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SnPrescriptionMedicine snPrescriptionMedicine)
    {
        List<SnPrescriptionMedicine> list = snPrescriptionMedicineService.selectSnPrescriptionMedicineList(snPrescriptionMedicine);
        ExcelUtil<SnPrescriptionMedicine> util = new ExcelUtil<SnPrescriptionMedicine>(SnPrescriptionMedicine.class);
        util.exportExcel(response, list, "记录患者处方药品数据");
    }

    /**
     * 获取记录患者处方药品详细信息
     */
    @PreAuthorize("@ss.hasPermi('sn:medicine:query')")
    @GetMapping(value = "/{prescriptionMedicineId}")
    public AjaxResult getInfo(@PathVariable("prescriptionMedicineId") Long prescriptionMedicineId)
    {
        return success(snPrescriptionMedicineService.selectSnPrescriptionMedicineByPrescriptionMedicineId(prescriptionMedicineId));
    }

    /**
     * 新增记录患者处方药品
     */
    @PreAuthorize("@ss.hasPermi('sn:medicine:add')")
    @Log(title = "记录患者处方药品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<SnPrescriptionMedicine> snPrescriptionMedicines)
    {
        int successCount = 0;

        // 循环逐条插入每条记录
        for (SnPrescriptionMedicine medicine : snPrescriptionMedicines) {
            int isInserted = snPrescriptionMedicineService.insertSnPrescriptionMedicine(medicine);
            if (isInserted>0) {
                successCount++;
            }
        }

        // 返回插入成功的记录数
        return toAjax(successCount);
    }


    /**
     * 修改记录患者处方药品
     */
    @PreAuthorize("@ss.hasPermi('sn:medicine:edit')")
    @Log(title = "记录患者处方药品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SnPrescriptionMedicine snPrescriptionMedicine)
    {
        return toAjax(snPrescriptionMedicineService.updateSnPrescriptionMedicine(snPrescriptionMedicine));
    }

    /**
     * 删除记录患者处方药品
     */
    @PreAuthorize("@ss.hasPermi('sn:medicine:remove')")
    @Log(title = "记录患者处方药品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{prescriptionMedicineIds}")
    public AjaxResult remove(@PathVariable Long[] prescriptionMedicineIds)
    {
        return toAjax(snPrescriptionMedicineService.deleteSnPrescriptionMedicineByPrescriptionMedicineIds(prescriptionMedicineIds));
    }
}
